package com.yiqi.zhiyuan.feature.mine.view;

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.TextView;

import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.widget.dialog.ActionSheetDialog;
import com.suozhang.framework.widget.dialog.OnOperItemClickL;
import com.yiqi.zhiyuan.R;
import com.yiqi.zhiyuan.common.lib.widget.FullyGridLayoutManager;
import com.yiqi.zhiyuan.feature.mine.contract.FeedbackContract;
import com.yiqi.zhiyuan.feature.mine.dependencies.feedback.DaggerFeedbackComponent;
import com.yiqi.zhiyuan.feature.mine.dependencies.feedback.FeedbackPresenterModule;
import com.yiqi.zhiyuan.feature.mine.view.adapter.GridImageAdapter;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @ProjectName: ZhiYuan
 * @Package: com.yiqi.zhiyuan.feature.mine.view
 * @ClassName: FeedBackActivity
 * @Description: java类作用描述
 * @Author:
 * @CreateDate: 2019/3/12 15:02
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/3/12 15:02
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class FeedBackActivity extends BaseActivity implements OnOperItemClickL, GridImageAdapter.OnItemClickListener,
        TextWatcher, FeedbackContract.View {
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.edt_feedback_content)
    EditText mEdtFeedbackContent;
    @BindView(R.id.recycler)
    RecyclerView mRecycler;
    @BindView(R.id.edt_feedback_phone)
    EditText mEdtFeedbackPhone;
    @BindView(R.id.tv_user_feedback_submit)
    TextView mTvUserInfoSubmit;
    @BindView(R.id.tv_feedback_content_count)
    TextView mTvFeedbackContentCount;
    private List<LocalMedia> selectList = new ArrayList<>();
    private GridImageAdapter adapter;
    private int maxSelectNum = 4;
    private ActionSheetDialog dialog;
    private int mUserId = -1;

    @Inject
    FeedbackContract.Presenter mPresenter;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_feed_back;
    }

    @Override
    protected void initView() {

        initToolBar(mToolbar, getString(R.string.feed_back_title), true, true);
        final String[] stringItems = {getString(R.string.common_photo_take_text), getString(R.string.common_photo_select_text)};
        dialog = new ActionSheetDialog(FeedBackActivity.this, stringItems, null);
        dialog.isTitleShow(false);
        dialog.setOnOperItemClickL(FeedBackActivity.this);

        initAdapter();

    }

    private GridImageAdapter.onAddPicClickListener onAddPicClickListener = new GridImageAdapter.onAddPicClickListener() {
        @Override
        public void onAddPicClick() {
            dialog.show();

        }

    };

    @Override
    protected void initInjector() {
        DaggerFeedbackComponent.builder().feedbackPresenterModule(new FeedbackPresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvent() {
        mEdtFeedbackContent.addTextChangedListener(this);
    }

    @Override
    public void onOperItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (dialog != null) dialog.dismiss();
        switch (position) {
            case 0:
                PictureSelector.create(FeedBackActivity.this)
                        .openCamera(PictureMimeType.ofImage())
                        .theme(R.style.picture_default_style)
                        .selectionMode(PictureConfig.MULTIPLE)// 多选 or 单选
                        .previewImage(false)// 是否可预览图片
                        .isZoomAnim(true)// 图片列表点击 缩放效果 默认true
                        .isCamera(true)
                        .maxSelectNum(maxSelectNum)
                        .enableCrop(false)// 是否裁剪
                        .compress(true)// 是否压缩
                        .freeStyleCropEnabled(true)// 裁剪框是否可拖拽
                        .selectionMedia(selectList)// 是否传入已选图片
                        .scaleEnabled(true)
                        .forResult(PictureConfig.CHOOSE_REQUEST);//结果回调onActivityResult code
                break;
            case 1:
                PictureSelector.create(FeedBackActivity.this)
                        .openGallery(PictureMimeType.ofImage())
                        .theme(R.style.picture_default_style)
                        .selectionMode(PictureConfig.MULTIPLE)// 多选 or 单选
                        .previewImage(false)// 是否可预览图片
                        .isZoomAnim(true)// 图片列表点击 缩放效果 默认true
                        .isCamera(false)
                        .maxSelectNum(maxSelectNum)
                        .enableCrop(false)// 是否裁剪
                        .compress(true)// 是否压缩
                        .freeStyleCropEnabled(true)// 裁剪框是否可拖拽
                        .selectionMedia(selectList)// 是否传入已选图片
                        .scaleEnabled(true)
                        .forResult(PictureConfig.CHOOSE_REQUEST);//结果回调onActivityResult code
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    // 图片选择结果回调
                    selectList = PictureSelector.obtainMultipleResult(data);


                    adapter.setList(selectList);
                    adapter.notifyDataSetChanged();
                    break;
            }
        }
    }

    private void initAdapter() {
        FullyGridLayoutManager manager = new FullyGridLayoutManager(FeedBackActivity.this, 4, GridLayoutManager.VERTICAL, false);
        mRecycler.setLayoutManager(manager);
        mRecycler.setHasFixedSize(false);
        mRecycler.setNestedScrollingEnabled(true);
        adapter = new GridImageAdapter(FeedBackActivity.this, onAddPicClickListener);
        adapter.setList(selectList);
        adapter.setSelectMax(maxSelectNum);
        mRecycler.setAdapter(adapter);
        adapter.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(int position, View v) {
        if (selectList.size() > 0) {
            LocalMedia media = selectList.get(position);
            String pictureType = media.getPictureType();
            int mediaType = PictureMimeType.pictureToVideo(pictureType);
            switch (mediaType) {
                case 1:
                    // 预览图片 可自定长按保存路径
                    //PictureSelector.create(FeedbackCommitActivity.this).themeStyle(themeId).externalPicturePreview(position, "/custom_file", selectList);
                    PictureSelector.create(FeedBackActivity.this).themeStyle(R.style.picture_default_style).openExternalPreview(position, selectList);
                    break;
                case 2:
                    // 预览视频
                    PictureSelector.create(FeedBackActivity.this).externalPictureVideo(media.getPath());
                    break;
                case 3:
                    // 预览音频
                    PictureSelector.create(FeedBackActivity.this).externalPictureAudio(media.getPath());
                    break;
            }
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        mTvFeedbackContentCount.setText(s.length() + "/100");
        if (!TextUtils.isEmpty(mEdtFeedbackContent.getText().toString().trim())) {
            mTvUserInfoSubmit.setEnabled(true);
        } else {
            mTvUserInfoSubmit.setEnabled(false);
        }
    }

    @Override
    public void showUploadImageSuccess(String imageUrl) {
        strings.add(imageUrl);
        if (strings.size() == selectList.size()) {
            mPresenter.doFeedback(
                    mEdtFeedbackContent.getText().toString().trim(),
                    mEdtFeedbackPhone.getText().toString().trim(),
                    strings);
        }
    }

    @Override
    public void showUploadImageError(String errorMsg) {
        showMsg(errorMsg);
    }

    @Override
    public void showFeedbackSuccess() {
        finish();
    }

    @Override
    public void showFeedbackError(String errorMsg) {
        showMsg(errorMsg);
    }

    List<String> strings = null;

    @OnClick(R.id.tv_user_feedback_submit)
    public void onViewFeedbackClicked() {
        strings = new ArrayList<>();
        if (selectList != null && selectList.size() > 0) {
            for (LocalMedia media : selectList) {
                mPresenter.uploadImage(media.getCompressPath());
            }
        }else {
            mPresenter.doFeedback(
                    mEdtFeedbackContent.getText().toString().trim(),
                    mEdtFeedbackPhone.getText().toString().trim(),
                    null);
        }
    }
}
